package com.ksd.pug.clound.controller;

import com.ksd.pug.clound.pojo.SysUser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/2/25 15:47
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/user/{id}")
    @HystrixCommand(fallbackMethod = "fallback", commandProperties = {
            // 如果请求并发数达到该设置的值，请求会被拒绝和抛出异常并且fallback不会被调用 默认是：10秒
            @HystrixProperty(name = HystrixPropertiesManager.FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value = "15")
    })
    public SysUser findUsers(@PathVariable("id") Long id) {
        if(id==1L){
            System.out.println(1 / 0);
        }
        return restTemplate.postForObject("http://kuangstudy-hystrix-user-service/api/user/" + id, null, SysUser.class);
    }


    public SysUser fallback(Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(-1L);
        return sysUser;
    }
}
